{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import seaborn as sns\n",
    "import regex as re\n",
    "import matplotlib.animation as animation\n",
    "import gc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "filepath = '../test/Validation_step/Output.log'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(filepath, 'r') as f:\n",
    "    data_string = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_string_lines = data_string.split('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del data_string\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-16-f07eb5fdfe42>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi_l\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ml\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_string_lines\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m     \u001b[0mline\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m     \u001b[0;32mif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"Reading\"\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mline\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"Species\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m         \u001b[0mnextIsSpecies\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mnextIsSpecies\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# stores tuple of process at each time step\n",
    "process = []\n",
    "nIter = 0\n",
    "nextIsSpecies = False\n",
    "SpeciesDict = {}\n",
    "nSpecies = 0\n",
    "for i_l, l in enumerate(data_string_lines):\n",
    "    line = l.split()\n",
    "    if (line[0] == \"Reading\" and line[1] == \"Species\"):\n",
    "        nextIsSpecies = True\n",
    "    elif (nextIsSpecies):\n",
    "        if (line[0] == \"Reading\"):\n",
    "            SpeciesDict[line[1]] = nSpecies\n",
    "            nSpecies = nSpecies + 1\n",
    "        else:\n",
    "            nextIsSpecies = False\n",
    "    elif (line[0] == \"Running\" and line[2] == \"iterations\"):\n",
    "        nIter = int(line[1])\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.030616543710627126"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "50000./len(data_string_lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_lines(lines):\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stores tuple of process at each time step\n",
    "process = [];\n",
    "nIter = 0;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open (filepath, \"r\")\n",
    "nextIsSpecies = False\n",
    "SpeciesDict = {}\n",
    "nSpecies = 0\n",
    "\n",
    "# Looking for the number of iterations\n",
    "for l in f:\n",
    "    line = l.split()\n",
    "    if (line[0] == \"Reading\" and line[1] == \"Species\"):\n",
    "        nextIsSpecies = True\n",
    "    elif (nextIsSpecies):\n",
    "        if (line[0] == \"Reading\"):\n",
    "            SpeciesDict[line[1]] = nSpecies\n",
    "            nSpecies = nSpecies + 1\n",
    "        else:\n",
    "            nextIsSpecies = False\n",
    "    elif (line[0] == \"Running\" and line[2] == \"iterations\"):\n",
    "        nIter = int(line[1])\n",
    "        f.close()\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dimensions of the lattice\n",
    "(x, y) = (160, 120)\n",
    "started = False\n",
    "ts = 0\n",
    "nextIsProcessName = False\n",
    "nextLatticeHeight = False\n",
    "#Initialize layer\n",
    "layer = []\n",
    "\n",
    "#Initialize results\n",
    "results = []\n",
    "\n",
    "f = open(filepath, \"r\")\n",
    "\n",
    "# Iterate through each time step\n",
    "for l in f:\n",
    "    line = l.split()\n",
    "    if ((not started) and line[0] == \"Time\"):\n",
    "        started = True\n",
    "    if (line[0] == \"Time\" and line[1] == \"step:\"):\n",
    "        ts = int(float(line[2]))\n",
    "        nextIsProcessName = True;\n",
    "        continue\n",
    "    elif (nextIsProcessName):\n",
    "        process.append((ts, line[0]))\n",
    "        nextIsProcessName = False\n",
    "    elif (not nextLatticeHeight and line[0] == \"---------------------------------------\"):\n",
    "        nextLatticeHeight = True\n",
    "    elif (nextLatticeHeight and line[0] == \"---------------------------------------\"):\n",
    "        results.append(layer)\n",
    "        layer = []\n",
    "        nextLatticeHeight = False\n",
    "    elif(nextLatticeHeight):\n",
    "        line = ' '.join(line)\n",
    "        # Split into substrings\n",
    "        parsed = re.findall(r'\\(.*?\\)', line) \n",
    "        heights = [x.split()[1] for x in parsed]\n",
    "        species_codes = [x[x.find(\"[\") + 1:x.find(\"]\")] for x in parsed]\n",
    "\n",
    "        for i in range(len(species_codes)):\n",
    "            if species_codes[i].strip():\n",
    "                species_codes[i] = species_codes[i].split()\n",
    "                species_codes[i] = [SpeciesDict[x] for x in species_codes[i]]\n",
    "        res2 = [(heights[i], species_codes[i]) for i in range(len(heights))]\n",
    "        layer.append(res2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#https://towardsdatascience.com/how-to-create-animated-graphs-in-python-bb619cc2dec1\n",
    "#https://stackoverflow.com/questions/33742845/how-to-animate-a-seaborns-heatmap-or-correlation-matrix\n",
    "%matplotlib notebook\n",
    "\n",
    "def init():\n",
    "    sns.heatmap(np.zeros((160, 120)), square=False, cbar=False)\n",
    "def animate(i):\n",
    "    plt.clf()\n",
    "    layer = results[i]\n",
    "    textList = []\n",
    "    temp = []\n",
    "    result = np.zeros((x, y))\n",
    "    for j in range(x):\n",
    "        for k in range(y):\n",
    "            result[j, k] = int(layer[j][k][0].replace(',', ''))\n",
    "            textList.append(layer[j][k][1])    \n",
    "        temp = []\n",
    "    text = np.array(textList, dtype=object)\n",
    "    labels = (np.asarray([\"{0}\".format(t) for t in text])).reshape(x,y)\n",
    "    ax = sns.heatmap(result, linewidth=0.5, annot=True)\n",
    "fig = plt.figure()\n",
    "ani = animation.FuncAnimation(fig, animate, init_func=init, frames=5, repeat=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9897\n"
     ]
    }
   ],
   "source": [
    "print(len(results))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
